﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using DataTransfer;

namespace BUS
{
  public  class PhieuKhamChiTietBus
    {
        public string connect = QuiDinhBus.connect;
        public static QLPMDataContext db;
        // db = new QLPMDataContext(connect);
        public IEnumerable<PhieuKhamChiTiet> GetAll()
        {
            IEnumerable<PhieuKhamChiTiet> result = null;
            try
            {
                db = new QLPMDataContext(connect);
                result = from t in db.PhieuKhamChiTiets select t;
                //db.Dispose();

            }
            catch (Exception)
            {

                throw;
            }
            return result;


        }
        public int Insert(PhieuKhamChiTiet info)
        {
            int id = -1;
            try
            {
                db = new QLPMDataContext(connect);
                db.PhieuKhamChiTiets.InsertOnSubmit(info);
                db.SubmitChanges();

                id = info.ID;
            }
            catch (Exception)
            {

                throw;
            }

            return id;
        }
        public PhieuKhamChiTiet Get(int key)
        {
            PhieuKhamChiTiet info = new PhieuKhamChiTiet();
            info = null;
            try
            {
                db = new QLPMDataContext(connect);
                info = (from qd in db.PhieuKhamChiTiets where qd.ID == key select qd).SingleOrDefault();
            }
            catch (Exception)
            {

                throw;
            }
            return info;
        }
        public bool Delele(int key)
        {
            bool flag = false;
            try
            {

                db = new QLPMDataContext(connect);
                PhieuKhamChiTiet del = (from t in db.PhieuKhamChiTiets where t.ID == key select t).SingleOrDefault();
                
               db.PhieuKhamChiTiets.DeleteOnSubmit(del);
                

                db.SubmitChanges();

                flag = true;
            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
        public bool DeleleById(int key)
        {
            bool flag = false;
            try
            {

                db = new QLPMDataContext(connect);

                
               var del = from t in db.PhieuKhamChiTiets where t.IDPhieuKham == key select t;
                foreach (var item in del)
                {
                    db.PhieuKhamChiTiets.DeleteOnSubmit(item);
                }
                

                db.SubmitChanges();

                flag = true;
            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
       
        public bool Update(PhieuKhamChiTiet info)
        {
            bool flag = false;
            try
            {
                db = new QLPMDataContext(connect);
                var t = (from qd in db.PhieuKhamChiTiets where qd.ID == info.ID select qd).SingleOrDefault();
                t.IDPhieuKham = info.IDPhieuKham;
                t.IDTHuoc = info.IDTHuoc;
                t.SoLuong = info.SoLuong;
                t.CachDung = info.CachDung;
                t.UserModify = info.UserModify;
                t.ModifyDate = info.ModifyDate;
                db.SubmitChanges();


                flag = true;

            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
    }
}
